<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property integer $wattage
 * @property string $universal_input
 * @property integer $lumens
 * @property string $lifetime
 * @property string $cct
 * @property integer $warranty
 * @property string $brands
 * @property string $powerfactor
 * @property string $product_pn
 * @property integer $unit
 */
class Product extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('brands,vendor_id,category_id', 'required'),
			array('wattage, lumens, warranty, unit_buy, unit_sell,vendor_id,category_id', 'numerical', 'integerOnly'=>true),
			array('universal_input, lifetime, cct, brands', 'length', 'max'=>30),
			array('powerfactor', 'length', 'max'=>29),
			array('product_pn', 'length', 'max'=>40),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('brands, powerfactor, product_pn, unit', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'vendorID' => array(self::BELONGS_TO, 'Vendor', 'vendor_id'),
			'categoryID' => array(self::BELONGS_TO, 'Category', 'category_id'),
			'quotationDetail' => array(self::BELONGS_TO, 'QuotationOrderDetail', 'product_pn'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'wattage' => 'WATTAGE',
			'universal_input' => 'UNIVERSAL INPUT',
			'lumens' => 'LUMENS',
			'lifetime' => 'LIFETIME',
			'cct' => 'CCT',
			'warranty' => 'WARRANTY',
			'brands' => 'BRANDS',
			'powerfactor' => 'POWERFACTOR',
			'product_pn' => 'PART NUMBER',
			'unit_buy' => 'UNIT BUY',
			'unit_sell' => 'UNIT SELL',
			'vendor_id'=>'VENDOR',
			'category_id'=>'CATEGORY',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('wattage',$this->wattage);
		$criteria->compare('universal_input',$this->universal_input,true);
		$criteria->compare('lumens',$this->lumens);
		$criteria->compare('lifetime',$this->lifetime,true);
		$criteria->compare('cct',$this->cct,true);
		$criteria->compare('warranty',$this->warranty);
		$criteria->compare('brands',$this->brands,true);
		$criteria->compare('powerfactor',$this->powerfactor,true);
		$criteria->compare('product_pn',$this->product_pn,true);
		$criteria->compare('unit_buy',$this->unit_buy);
		$criteria->compare('unit_sell',$this->unit_sell);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave() {

	    if (parent::beforeSave()) {

	        //$this->status_delete='-';
	        /*$Vendor= Vendor::model()->findAll('vendor=:vendor',array(':vendor'=>$quo_no));
	        $this->employee_id=Yii::app()->session['id'];
	        $this->generate=0;*/
	        
	        return true;
	    }
	}
}
